#pragma once

#include "Sol9Types.h"
#include "RTTI.h"
#include <cassert>

namespace sol9
{

	class IReferenceCounter
	{
	private:
		mutable s32 ref;

	public:
		IReferenceCounter() : ref(1) {}
		virtual ~IReferenceCounter() {}		// for delete this

		void attach() const
		{
			++ref;
		}

		bool detach() const
		{
			// someone is doing bad reference counting
			assert(ref > 0 && "<IReferenceCounter> counting error");

			if (--ref == 0)
			{
				delete this;
				return true;
			}

			return false;
		}

		s32 getReferenceCount() const
		{
			return ref;
		}
	};

} // namespace sol9